package com.example.books.tools;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;

public class GetResoures {
    /**
     * 读取Assets文件夹中的图片资源 
     * @param context 
     * @param fileName 图片名称 
     * @return 
     */
    public static Bitmap GetImage(Context context, String fileName) {
        Bitmap image =null;
        AssetManager am = context.getResources().getAssets();
        InputStream is = null;
        try {
            is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 读取Assets文件夹中的txt资源 
     * @param context
     * @param fileName 书路径
     * @return
     */
    public static String GetBook(Context context, String fileName) {
        try {
            InputStream is = context.getResources().getAssets().open(fileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String text = new String(buffer,"utf-8");
            return text;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "读取错误";
    }

}
